#include "../include/muduo/proto/codec.h"
#include "../include/muduo/proto/dispatcher.h"
#include "../include/muduo/base/Logging.h"
#include "../include/muduo/base/Mutex.h"
#include "muduo/net/EventLoop.h"
#include "muduo/net/TcpServer.h"

#include "request.pb.h"
#include <iostream>
#include <unordered_map>

class Server
{
public:
    typedef std::shared_ptr<google::protobuf::Message> MessagePtr;
    typedef std::shared_ptr<lrl::TranslateRequest> TranslateRequestPtr;
    typedef std::shared_ptr<lrl::AddRequest> AddRequestPtr;

    Server(int port)
        : _server(&_baceloop, muduo::net::InetAddress("0.0.0.0", port), "Server", muduo::net::TcpServer::kNoReusePort),
          _dispatcher(std::bind(&Server::onUnknownMessage, this, std::placeholders::_1, std::placeholders::_2,
                                std::placeholders::_3)),
          _codec(std::bind(&ProtobufDispatcher::onProtobufMessage, &_dispatcher, std::placeholders::_1, std::placeholders::_2,
                           std::placeholders::_3))
    {
        // 注册业务请求处理函数
        _dispatcher.registerMessageCallback<lrl::TranslateRequest>(std::bind(&Server::onTranslate, this, std::placeholders::_1, std::placeholders::_2,
        std::placeholders::_3));
        _dispatcher.registerMessageCallback<lrl::AddRequest>(std::bind(&Server::onAdd,this,std::placeholders::_1, std::placeholders::_2,
        std::placeholders::_3));
        _server.setMessageCallback(std::bind(&ProtobufCodec::onMessage,&_codec,std::placeholders::_1, std::placeholders::_2,
        std::placeholders::_3));
        _server.setConnectionCallback(std::bind(&Server::onConnection,this,std::placeholders::_1));
    }
    void Start()
    {
        _server.start();
        _baceloop.loop();
    }

private:
    std::string translate(const std::string &str) 
    {
        static::std::unordered_map<std::string,std::string> hash = 
        {
            {"hello","你好"},
            
             {"who","谁"},
             {"what","谁"},
             {"why","为什么"},
             {"eat","吃"}
        };
        auto it = hash.find(str);
        if(it == hash.end())
        {
            return "没听懂";
        }
        return it->second;
    }

    void onTranslate(const muduo::net::TcpConnectionPtr &conn, const TranslateRequestPtr &message, muduo::Timestamp)
    {
        //获取信息
        std::string resq = message->msg();
        
        std::string rsq_msg = translate(resq);

        lrl::TranslateRespone resp;
        resp.set_msg(resp);

        _codec.send(conn,resp);

    }

    void onAdd(const muduo::net::TcpConnectionPtr &conn, const AddRequestPtr &message, muduo::Timestamp) 
    {
        int num1 = message->num1();
        int num2 = message->num2();
        int result = num1 + num2;
        lrl::AddRespone resp;
        resp.set_result(result);
        _codec.send(conn,resp);
    }

    void onConnection(const muduo::net::TcpConnectionPtr &conn)
    {
        if(conn->connected())
        {
            LOG_INFO<<"连接成功";

        }
        else
        {
            LOG_INFO<<"连接即将关闭";
        }
    }

    void onUnknownMessage(const muduo::net::TcpConnectionPtr &conn, const MessagePtr &message, muduo::Timestamp)
    {
        LOG_INFO << "onUnknow" << message->GetTypeName();
        conn->shutdown();
    }

private:
    muduo::net::EventLoop _baceloop;
    muduo::net::TcpServer _server;
    ProtobufDispatcher _dispatcher;
    ProtobufCodec _codec;
};

int main()
{
    Server _server(8081);
    _server.Start();
    return 0;
}